package com.gopay.common.cps.dao.order.impl;

import com.gopay.common.base.dao.impl.BaseReadDAOImpl;
import com.gopay.common.cps.dao.order.CpsGeneralFundOrderQueryDAO;
import com.gopay.common.domain.fund.FundGopayCpsGenOrder;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询类
 * 2016-08-19
 */
@Repository(value="cpsGeneralFundOrderQueryDAO")
public class CpsGeneralFundOrderQueryDAOImpl extends BaseReadDAOImpl<FundGopayCpsGenOrder, Serializable> implements
        CpsGeneralFundOrderQueryDAO {

    /**
     * 查询
     * @param start
     * @param end
     * @param type
     * @param orderType
     * @return
     */
    @Override
    public List<FundGopayCpsGenOrder> findOrder(String start, String end, String[] type, String orderType){

        StringBuilder sb = new StringBuilder();

        if(start!=null && !"".equals(start)) {
            StringBuilder typeS = new StringBuilder();
            for(int i = 0; i < type.length; i++) {
                if(type.length-1==i) {
                    typeS.append("'"+type[i]+"'");
                }else {
                    typeS.append("'"+type[i]+"'"+",");
                }
            }
            if(orderType!=null){
                sb.append(" from  FundGopayCpsGenOrder t where   to_char(t.stlmDate,'yyyyMMdd')=:stlmDate  " +
                        "  and t.tranType in (" + typeS.toString() + ") and  t.orderType=" + "'" + orderType + "'");

            }else {
                sb.append(" from  FundGopayCpsGenOrder t where   to_char(t.stlmDate,'yyyyMMdd')=:stlmDate  " +
                        "  and t.tranType in (" + typeS.toString() + ")");
            }

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("stlmDate",start);
            List<FundGopayCpsGenOrder> byHql = findByHql(sb.toString(),map);
            return byHql;

        }
        return null;
    }

    /**
     * 查询
     * @param start
     * @param end
     * @param type
     * @param ordertpey
     * @param custId
     * @return
     */
    @Override
    public List<FundGopayCpsGenOrder> findOrder(String start, String end, String[] type, String ordertpey, String custId) {
        StringBuilder sb = new StringBuilder();
        StringBuilder typeS = new StringBuilder();
        for(int i = 0; i < type.length; i++) {
            if(type.length-1==i) {
                typeS.append("'"+type[i]+"'");
            }else {
                typeS.append("'"+type[i]+"'"+",");
            }
        }
        if(ordertpey!=null &&  custId!=null ) {
            sb.append(" from  FundGopayCpsGenOrder t where to_char(t.stlmDate,'yyyyMMdd')=:stlmDate  " +
                    "  and t.merchantId=:merchantId and t.orderType=" + "'" + ordertpey + "' and t.tranType in (" + typeS.toString() + ")");
        }
        if(ordertpey==null && custId!=null){
            sb.append(" from  FundGopayCpsGenOrder t where   to_char(t.stlmDate,'yyyyMMdd')=:stlmDate  " +
                    "  and t.merchantId=:merchantId  and t.tranType in (" + typeS.toString() + ") ");
        }
        if(ordertpey!=null && custId==null){
            sb.append(" from  FundGopayCpsGenOrder t where  to_char(t.stlmDate,'yyyyMMdd')=:stlmDate  " +
                    "    and t.tranType in (" + typeS.toString() + ") ");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stlmDate",start);
        if(custId!=null){
            map.put("merchantId",custId);
        }
        List<FundGopayCpsGenOrder> byHql = findByHql(sb.toString(),map);
        if(byHql!=null && byHql.size()>0) {
            return byHql;
        }
        return null;
    }

    @Override
    public FundGopayCpsGenOrder findByGopayOrderId(String gopayOrderId) {
        String hql = " from FundGopayCpsGenOrder order where order.gopayOrderId = :gopayOrderId";
        Map params = new HashMap();
        params.put("gopayOrderId", gopayOrderId);
        List<FundGopayCpsGenOrder> res = super.findByHql(hql, params);
        if (res != null && res.size() > 0) {
            return res.get(0);
        }
        return null;
    }

}
